#include <algorithm>
#include <cstdio>
#define lowbit(x) ((-x) & x)

const int N = 3000010;
int n, cnt, id, tot;
int a[N], heads[N], val[N], ans[N];
int size[N], son[N], fath[N], top[N], deep[N];

struct Edge {
	int v, nxt;
} e[N << 1];
inline void add(int u, int v) {
	e[++cnt].nxt = heads[u];
	heads[u] = cnt;
	e[cnt].v = v;
}

void dfs1(int x, int fa) {
	size[x] = 1, deep[x] = deep[fa] + 1;
	fath[x] = fa;
	for (int i = heads[x]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (v == fa)
			continue;
		dfs1(v, x);
		size[x] += size[v];
		if (size[v] > size[son[x]])
			son[x] = v;
	}
}

void dfs2(int x, int topp) {
	top[x] = topp;
	if (son[x]) {
		dfs2(son[x], topp);
	}
	for (int i = heads[x]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (v == fath[x] || v == son[x])
			continue;
		dfs2(v, v);
	}
}

void dfs3(int x, int fa) {
	ans[x] = val[x];
	for (int i = heads[x]; i; i = e[i].nxt) {
		if (e[i].v == fa) continue;
		dfs3(e[i].v, x);
		ans[x] += ans[e[i].v];
	}
}

inline void work(int x, int y) {
	val[x]++, val[y]++;
	while (top[x] != top[y]) {
		if (deep[top[x]] < deep[top[y]]) std::swap(x, y);
		x = fath[top[x]];
	}
	if (deep[x] < deep[y]) std::swap(x, y);
	val[y]--;
	if (fath[y])
		val[fath[y]]--;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
#endif
#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	int t1, t2;
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &t1, &t2);
		add(t1, t2);
		add(t2, t1);
	}
	dfs1(a[1], 0);
	dfs2(a[1], 0);
	for (int i = 1; i < n; ++i) {
		work(a[i], a[i + 1]);
	}
	dfs3(a[1], 0);
	for (int i = 1; i <= n; ++i) {
		printf("%d\n", ans[i] - (i != a[1]));
	}
	return 0;
}